#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec  5 11:00:46 2023

@author: njr8582
"""

###This script replicates Figure C1 in the online appendix. It first recreates the left-panel and then the right panel.

###Load packages
import numpy as np
import matplotlib.pyplot as plt

###Define primitives
#For country i
k_i = 0 
a_i = 0.3 
w_bar_i = 0
w_min_i = -0.8 

#For country j
k_j = 0
a_j = 0.3
w_bar_j = 0
w_min_j = -0.8 

###We now start solving for the equilibrium.


#First we want to find the rate at which FBeta_i decreases with time
#Assuming a uniform CDF, this is the denominator for all calculations of the cdf
range_i = w_bar_i - w_min_i
range_j = w_bar_j - w_min_j

#The following is the set of types that must finish exiting if the horizon date is at time t

def betaipeace(t):
    return (-a_i*t - w_min_i)/range_i

def betajpeace(t):
    return (-a_j*t - w_min_j)/range_j

###The following function returns the CDF for how many types have conceded by time t if there is no
#  mass of types that concede at time 0 and countries play according to their concession rates in the peaceful phase. 

def HDC_i(t):
    hdc = 1 - (1/(1+a_j*t))**(((a_j+k_j)/a_j))
    return hdc

###Our goal is now to figure out where each country has the two above quantities intersect

#Before proceeding any further it is useful to circumscribe ourselves to the set of times that are realistic.
#We'll restrict ourselves to when F_beta_ would hit w_min because audience costs grew too large
t_max = (-w_min_i)/a_i 

t = np.linspace(0, t_max, 1000)

#Now to find the intersection of the two functions
y1i = betaipeace(t)
y2i = HDC_i(t)

idxi = np.argwhere(np.diff(np.sign(y1i - y2i))).flatten() #y1i is strictly decreasing, while y2i is strictly increasing, this returns when they cross
hdc_i = t[idxi] #hdc_i is the candidate date for when the peaceful phase ends in the horizon date. 

#Next we want to plot the strategies for this figure
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Simulating Fearon (1994)')
ax1.title.set_text(r'$\bar{w}_i = 0$')

T1 = hdc_i

#We start by setting the limits for the figure, which are the same for both countries
endpoint_fig = T1 + T1*0.1 #the range of x in the figure should depend on how long it takes to get to the horizon date 
endpoint_q = T1/endpoint_fig #used bellow to plot Q_i(t)
ax1.set_xlim(0, endpoint_fig) 
ax1.set_ylim(w_min_i, (w_bar_i+0.2)) 


#Then we move onto plotting the concession strategy for each country
Beta_i = betaipeace(T1) *range_i + w_min_i #Because the countries are playing mixed strategies, it's only necessary to draw a line
y_range_i = range_i + 0.2

ax1.axhline(y = Beta_i, xmin = 0, xmax = endpoint_q[0]) #Plotting Q_i(t)
ax1.axvline(x = T1, ymin = 0, ymax = ((Beta_i[0] - w_min_i)/y_range_i), ls = '--') #Dashed line when the concessions are done
ax1.fill_between(x = [0,T1[0]], y1 = Beta_i, y2=w_min_i, facecolor = 'none', edgecolor = 'b' ,hatch = '//')
ax1.annotate(r'$\beta_i$', xy = (0,Beta_i) , xytext = ((T1/9),(Beta_i+y_range_i*0.1)), arrowprops=dict(arrowstyle = 'simple'))



#Then we move onto plotting the strategies the mass of types going to war
ax1.axvline(x = T1, ymin = ((Beta_i[0]-w_min_i)/y_range_i), ymax = ((w_bar_i-w_min_i)/(y_range_i)), color = 'r') #Plotting wars at the horizon date
ax1.axhline(y = w_bar_i , xmin = 0, xmax = endpoint_q[0], ls = '--', color = 'r') #Dashed line up to the point where war begins
ax1.annotate(r'$\widebar{w}_i$', xy = (0,w_bar_i) , xytext = ((T1[0]/9),(w_bar_i -y_range_i*0.1)), 
             arrowprops=dict(arrowstyle = 'simple', facecolor = 'red'))

#
####Now onto the right panel
##Define primitives
#For country i
w_bar_i = 0.8

#For country j
w_bar_j = 0.8

## We must reproduce the functions above

range_i = w_bar_i - w_min_i
range_j = w_bar_j - w_min_j

def betaipeace(t):
    return (-a_i*t - w_min_i)/range_i

def HDC_i(t):
    hdc = 1 - (1/(1+a_j*t))**(((a_j+k_j)/a_j))
    return hdc

### we also need to consider a new set of times
t_max = (-w_min_i)/a_i 

t = np.linspace(0, t_max, 1000)

#now to calculate the intersections again
y1i = betaipeace(t)
y2i = HDC_i(t)

idxi = np.argwhere(np.diff(np.sign(y1i - y2i))).flatten() #y1i is strictly decreasing, while y2i is strictly increasing, this returns when they cross
hdc_i = t[idxi] #hdc_i is the candidate date for when the peaceful phase ends in the horizon date. 

#And finally, the plot
T1 = hdc_i

#We start by setting the limits for the figure, which are the same for both countries
endpoint_fig = T1 + T1*0.1 #the range of x in the figure should depend on how long it takes to get to the horizon date 
endpoint_q = T1/endpoint_fig #used bellow to plot Q_i(t)

Beta_i = betaipeace(T1) *range_i + w_min_i #Because the countries are playing mixed strategies, it's only necessary to draw a line
y_range_i = range_i + 0.2

ax2.title.set_text(r'$\bar{w}_i = 0.8$')
ax2.set_xlim(0, endpoint_fig) 
ax2.set_ylim(w_min_i, (w_bar_i+0.2)) 

ax2.axhline(y = Beta_i, xmin = 0, xmax = endpoint_q[0]) #Plotting Q_i(t)
ax2.axvline(x = T1, ymin = 0, ymax = ((Beta_i[0] - w_min_i)/y_range_i), ls = '--') #Dashed line when the concessions are done
ax2.fill_between(x = [0,T1[0]], y1 = Beta_i, y2=w_min_i, facecolor = 'none', edgecolor = 'b' ,hatch = '//')
ax2.annotate(r'$\beta_i$', xy = (0,Beta_i) , xytext = ((T1/9),(Beta_i+y_range_i*0.1)), arrowprops=dict(arrowstyle = 'simple'))

#Country 2
ax2.axvline(x = T1, ymin = ((Beta_i[0]-w_min_i)/y_range_i), ymax = ((w_bar_i-w_min_i)/(y_range_i)), color = 'r') #Plotting wars at the horizon date
ax2.axhline(y = w_bar_i , xmin = 0, xmax = endpoint_q[0], ls = '--', color = 'r') #Dashed line up to the point where war begins
ax2.annotate(r'$\widebar{w}_i$', xy = (0,w_bar_i) , xytext = ((T1[0]/9),(w_bar_i -y_range_i*0.1)), 
             arrowprops=dict(arrowstyle = 'simple', facecolor = 'red'))

plt.savefig(fname = 'figure1.pdf')
plt.show()

